<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImageUrl('user')" alt="user" class="user" />
          <div class="user-info">
            <p class="user-info-admin">Admin</p>
            <p class="user-info-p">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间:<span>2025-04-13</span></p>
          <p>上次登录地点:<span>南京</span></p>
        </div>
      </el-card>

      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData">
          <el-table-column
            v-for="(val, key) in tableLabel"
            :key="key"
            :prop="key"
            :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>

    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          shadow="always"
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            :is="item.icon"
            class="icons"
            :style="{ background: item.color }"
          ></component>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card class="top-chart">
        <div ref="echart" style="height: 280px"></div>
      </el-card>
      <div class="graph">
        <el-card>
          <div ref="userEchart" style="height: 240px"></div>
        </el-card>
        <el-card>
          <div ref="videoEchart" style="height: 240px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { useAllDataStore } from '@/stores'
import * as echarts from 'echarts'
import { getCurrentInstance, onMounted, reactive, ref } from 'vue'

const { proxy } = getCurrentInstance()

const store = useAllDataStore()
const getImageUrl = store.getImageUrl

// 1. 表格数据
const tableData = ref([])
const tableLabel = ref({
  name: '课程',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买',
})
const getTableData = async () => {
  const data = await proxy.$api.getTableData()
  tableData.value = data
}

// 2. 统计数据
const countData = ref([])
const getCountData = async () => {
  const data = await proxy.$api.getCountData()
  countData.value = data
}

// 3. 统计图表数据
const observer = ref(null)
// echarts 配置
const xOptions = reactive({
  // 图例文字颜色
  textStyle: {
    color: '#333',
  },
  legend: {},
  grid: {
    left: '20%',
  },
  // 提示框
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow',
    },
  },
  // x轴
  xAxis: {
    // 类目轴
    type: 'category',
    data: [],
    axisLine: {
      lineStyle: {
        color: '#17b3a3',
      },
    },
    axisLabel: {
      interval: 0,
      textStyle: {
        color: '#333',
      },
    },
  },
  // y轴
  yAxis: [
    {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#17b3a3',
        },
      },
    },
  ],
  color: ['#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80', '#8d98b3'],
  series: [],
})

const pieOptions = reactive({
  // 提示框
  tooltip: {
    trigger: 'item',
  },
  legend: {},
  color: [
    '#0f78f4',
    '#dd536b',
    '#9462e5',
    '#a6a6a6',
    '#e1bb22',
    '#39c362',
    '#3ed1cf',
  ],
  series: [],
})

const getChartData = async () => {
  const { date, orderData, userData, videoData } =
    await proxy.$api.getChartData()

  // 第一个
  // x轴
  xOptions.xAxis.data = date
  xOptions.series = Object.keys(orderData[0]).map((val) => {
    return {
      name: val,
      type: 'line',
      data: orderData.map((item) => item[val]),
    }
  })

  const oneEchart = echarts.init(proxy.$refs['echart'])
  oneEchart.setOption(xOptions)

  // 第二个
  xOptions.xAxis.data = userData.map((val) => val.date)
  xOptions.series = [
    {
      name: '新增用户',
      type: 'bar',
      data: userData.map((item) => item.new),
    },
    {
      name: '活跃用户',
      type: 'bar',
      data: userData.map((item) => item.active),
    },
  ]
  const twoEchart = echarts.init(proxy.$refs['userEchart'])
  twoEchart.setOption(xOptions)

  // 第三个
  pieOptions.series = [
    {
      data: videoData,
      type: 'pie',
    },
  ]
  const threeEchart = echarts.init(proxy.$refs['videoEchart'])
  threeEchart.setOption(pieOptions)

  // 监听页面变化, 调整尺寸
  // 尺寸变化回调函数
  observer.value = new ResizeObserver((en) => {
    // 获取echarts实例
    oneEchart.resize()
    twoEchart.resize()
    threeEchart.resize()
  })

  // 容器存在, 回调调整尺寸
  if (proxy.$refs['echart']) {
    observer.value.observe(proxy.$refs['echart'])
  }
}

onMounted(() => {
  getTableData()
  getCountData()
  getChartData()
})
</script>

<style lang="less" scoped>
.home {
  height: 100%;
  overflow: hidden;
  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;
    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
    .user-info {
      p {
        line-height: 40px;
      }
      .user-info-admin {
        font-size: 35px;
        color: #333;
      }
      .user-info-p {
        color: #999;
      }
    }
  }
  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;
      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }
  .user-table {
    margin-top: 20px;
  }

  .num {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
    .icons {
      width: 80px;
      height: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }
    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      .num {
        font-size: 30px;
        margin-bottom: 10px;
      }
      .txt {
        font-size: 15px;
        text-align: center;
        color: #999;
      }
    }
  }

  .graph {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    .el-card {
      width: 48%;
      height: 260px;
    }
  }
}
</style>
